using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDanhMucWeb()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCWEB
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteDanhMucWeb(int id, int page = 1)
		{
			var result = _repDanhMucWeb.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCWEB,
				CurrentPage = page
			};
			return View("SelectDanhMucWeb", CreateViewModel(data));
		}

		public ActionResult DetailOfDanhMucWeb(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DANHMUCWEB,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDanhMucWeb(FormCollection forms)
		{
            string id = forms.GetValues("DanhMucWeb_ID")[0];
            string tendanhmuc = forms.GetValues("DanhMucWeb_TenDanhMuc")[0];
            string gioithieu = forms.GetValues("DanhMucWeb_GioiThieu")[0];
            var file = Request.Files["DanhMucWeb_HinhMinhHoa"];

            string refName = "";
            var editItem = _repDanhMucWeb.SelectByID(int.Parse(id));
            editItem.TenDanhMuc = tendanhmuc;
            editItem.GioiThieu = gioithieu;

            var result = false;
            if (file.ContentLength != 0)
            {
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
                editItem.HinhMinhHoa = refName;
            }
            result = _repDanhMucWeb.Save();
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DANHMUCWEB,
                IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
                InfoText = result ? "Item has been updated" : "Cannot update this item"
            };
            return View("DetailOfDanhMucWeb", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDanhMucWeb(FormCollection forms)
		{
            string tendanhmuc = forms.GetValues("DanhMucWeb_TenDanhMuc")[0];
            string gioithieu = forms.GetValues("DanhMucWeb_GioiThieu")[0];
            var file = Request.Files["DanhMucWeb_HinhMinhHoa"];

            string refName = "";
            var newItem = new DanhMucWeb
            {
                TenDanhMuc = tendanhmuc,
                GioiThieu = gioithieu,
                HinhMinhHoa = "noimages.jpg"
            };

            var result = _repDanhMucWeb.Insert(newItem);
            if (result)
            {
                if (file.ContentLength != 0)
                {
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
                    if (result)
                    {
                        var addedItem = _repDanhMucWeb.SelectByID(newItem.ID);
                        addedItem.HinhMinhHoa = refName;
                        _repDanhMucWeb.Save();
                    }
                }
            }
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DANHMUCWEB,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectDanhMucWeb", CreateViewModel(data));
		}


	}
}